<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>权益管理</cite></a>
    <a><cite>实物权益</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_product_search">
        <script type="text/html" template>
          <div class="layui-inline">
            <div class="layui-form-label">关键字搜索</div>
            <div class="layui-input-inline">
              <input type="text" name="keyword" value="{{decodeURIComponent(layui.router().search.keyword || '')}}"
                     class="layui-input"
                     placeholder="请输入关键字">
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_product_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_product_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
          </div>
        </script>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_product"></div>
      <script type="text/html" id="tpl_name">
        {{# if (d.test_only) { }}
        <span class="layui-badge">测试</span>
        {{# } }}
        {{d.name}}
      </script>
      <script type="text/html" id="tpl_status">
        {{# if (d.status.value === 'init') { }}
        <span class="layui-font-gray">{{d.status.description}}</span>
        {{# } }}
        {{# if (d.status.value === 'on_sale') { }}
        <span class="layui-font-green">{{d.status.description}}</span>
        {{# } }}
        {{# if (d.status.value === 'pause') { }}
        <span class="layui-font-gray">{{d.status.description}}</span>
        {{# } }}
      </script>
      <script type="text/html" id="tpl_tool">

        <a class="layui-table-link"  lay-event="delivery_export">
          下载购买记录
        </a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.use(['fox_table'], function () {
    let $ = layui.$
      , fox_table = layui.fox_table
      , form = layui.form
      ,admin = layui.admin

    let resourceUrl = 'agent-admin/products_with_entity_rights'

    // 列表查询
    fox_table.render({
      id: 'table_product'
      , url: resourceUrl
      , cols: [[
        {title: 'ID', field: 'id', width: 80, align: "center", sort: true}
        , {title: '商品名称', templet: '#tpl_name', minWidth: 200}
        , {title: '发售库存', field: 'left_count', width: 100}
        , {title: '销售量', field: 'sales_count', width: 80}
        , {title: '状态', templet: '#tpl_status', width: 100}
        , {title: '操作', width: 200, align: 'left', fixed: 'right', toolbar: '#tpl_tool'}
      ]]
    });


    fox_table.toolbar(function (obj) {
      if(obj.event == 'delivery_export' ){
        layer.load();
        admin.post('agent-admin/export_product_buyers' ,obj.data,function (res) {
          layer.confirm('数据下载中，请到下载中心查看下载任务进度.', function (index) {
            layer.close(index);
            location.hash = '/ops/download';
            return false;
          });
          layer.closeAll('loading');
        }, {hasLoading: true})
      }
      return false;
    });

    //搜索
    form.on('submit(submit_product_search)', function (d) {
      fox_table.reload(d.field);
      return false;
    });
    //重置
    form.on('submit(reset_product_search)', function (d) {
      $("input[name='keyword']").val('');
      fox_table.reload({});
      return false;
    });
  })
</script>

